package com.yingxin.yx.framework.security.handler;


import com.yingxin.yx.framework.jwt.TokenSupport;
import com.yingxin.yx.framework.security.user.PrincipalDto;
import com.yingxin.yx.framework.security.userdetails.WexinUserDetailsService;
import com.yingxin.yx.framework.user.dto.TokenDto;
import com.yingxin.yx.framework.web.ResultUtil;
import com.yingxin.yx.framework.web.req.HttpHelper;
import com.yingxin.yx.framework.web.resp.RespHttp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 登录成功处理类
 */
@Component
public class WexinLoginSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private TokenSupport tokenSupport;

    @Autowired(required = false)
    private WexinUserDetailsService wexinUserDetailsService;

    /**
     * 登录成功返回结果
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        System.out.println("登录成功");
        // 组装JWT
        /*
        dA8jX9o3c4eZfbFXowBnpCsTWQSroAU+mhRKjwRfQUyUpzefE2IKBOqUUG+k6Uq8Jg9zxS/H0wWpVAygR5sRBSy7G0ZBL28gO6n3AvKvmqUD5zxrD2qX7AUPGLtYjzw+qotEHEZFXtj7obWpbNcmQYmW4lovGWocbGgrxn3Z/K8=

        SelfUserEntity selfUserEntity =  (SelfUserEntity) authentication.getPrincipal();
        String token = JWTTokenUtil.createAccessToken(selfUserEntity);
        token = JwtConfig.tokenPrefix + token;

        // 封装返回参数
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("code","200");
        resultData.put("msg", "登录成功");
        resultData.put("token",token);
        ResultUtil.responseJson(response,resultData);
        */

        PrincipalDto principal = (PrincipalDto) authentication.getPrincipal();

        // JwtTokenPair tokenPair = jwtTokenPairFactory.create(principal);
        // TokenDto tokenDto = buildTokenDto(principal.getUserId(), tokenPair);

        TokenDto tokenDto = tokenSupport.getAccessToken(principal);
        tokenDto.setAvatar(principal.getAvatar());

        // 登录日志
        wexinUserDetailsService.loginSuccessLog(principal, HttpHelper.getRequestInfo(request));

        ResultUtil.responseJson(request, response, RespHttp.ok(tokenDto));

        /*
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletResponse.setStatus(HttpServletResponse.SC_OK);
        servletResponse.setContentType("application/json");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setHeader("Access-Control-Allow-Credentials", "true");
        servletResponse.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        try {
            servletResponse.getWriter().write(JacksonHelper.toJson(resp));
        } catch (IOException e1) {
            // log.error(e1.getMessage(), e1);
        }
        */

    }

}